package com.ruoyi.hazard.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.file.domain.FileInfo;
import com.ruoyi.file.service.FileInfoService;
import com.ruoyi.hazard.domain.HazardTrackInfo;
import com.ruoyi.hazard.domain.vo.HazardTrackInfoVo;
import com.ruoyi.hazard.mapper.HazardTrackInfoMapper;
import com.ruoyi.hazard.service.HazardTrackInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Slf4j
@Service("hazardTrackInfoService")
public class HazardTrackInfoServiceImpl extends ServiceImpl<HazardTrackInfoMapper, HazardTrackInfo> implements HazardTrackInfoService {

    @Autowired
    private FileInfoService fileInfoService;
    @Override
    public HazardTrackInfoVo toVo(HazardTrackInfo hazardTrackInfo) {
        HazardTrackInfoVo vo = new HazardTrackInfoVo();
        BeanUtils.copyBeanProp(vo,hazardTrackInfo, "attachments");
        String attachments = hazardTrackInfo.getAttachments();
        if(StringUtils.hasText(attachments)){
            Long[] fileIds = Arrays.stream(attachments.split(",")).map(Long::parseLong).toArray(Long[]::new);
            List<FileInfo> fileInfos = fileInfoService.listByIds(Arrays.asList(fileIds));
            vo.setAttachments(fileInfos);
        }
        return vo;
    }

    @Override
    public HazardTrackInfo toEntity(HazardTrackInfoVo hazardTrackInfoVo) {
        HazardTrackInfo hazardTrackInfo = new HazardTrackInfo();
        BeanUtils.copyBeanProp(hazardTrackInfo,hazardTrackInfoVo, "attachments");
        List<FileInfo> attachments = hazardTrackInfoVo.getAttachments();
        if(attachments != null && !attachments.isEmpty()){
            hazardTrackInfo.setAttachments(StringUtils.join(attachments.stream().map(FileInfo::getId).toArray(), ","));
        }
        return hazardTrackInfo;
    }
}
